package com.example.lianjuan.ui.activeity;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

import com.example.lianjuan.Base.BaseActivity;
import com.example.lianjuan.Base.BaseFragment;
import com.example.lianjuan.R;
import com.example.lianjuan.Utils.LogUtils;
import com.example.lianjuan.databinding.ActivityMainBinding;
import com.example.lianjuan.ui.frament.HomeFragment;
import com.example.lianjuan.ui.frament.OnSellFragment;
import com.example.lianjuan.ui.frament.SearchFragment;
import com.example.lianjuan.ui.frament.SelectFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends BaseActivity implements IMainActivity {
    private ActivityMainBinding binding;
    private BottomNavigationView bottomNavigationView;
    private HomeFragment mHomeFragment;
    private OnSellFragment mOnSellFragment;
    private SearchFragment mSearchFragment;
    private SelectFragment mSelectFragment;
    private FragmentManager mFragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        View rootView = binding.getRoot();
        setContentView(rootView);
        initView();
        initFragment();
        initListener();
        switchFragment(mHomeFragment);
    }


    @Override
    protected void initListener() {
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if (item.getItemId() == R.id.home) {
                    LogUtils.d(MainActivity.this, "onNavigationItemSelected:--->home");
                    switchFragment(mHomeFragment);
                } else if (item.getItemId() == R.id.selected) {
                    LogUtils.d(MainActivity.this, "onNavigationItemSelected:--->selected");
                    switchFragment(mSelectFragment);
                } else if (item.getItemId() == R.id.red_packet) {
                    LogUtils.d(MainActivity.this, "onNavigationItemSelected:--->red_packet");
                    switchFragment(mOnSellFragment);
                } else if (item.getItemId() == R.id.search) {
                    LogUtils.d(MainActivity.this, "onNavigationItemSelected:--->search");
                    switchFragment(mSearchFragment);
                }
                return true;
            }
        });
    }

    private Fragment lastOneFragment = null;

    private void switchFragment(BaseFragment targetFragment) {
        //当切换的fragment跟当前显示的fragment相同，不操作
        if (lastOneFragment == targetFragment) {
            return;
        }
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        if (targetFragment.isAdded()) {
            transaction.show(targetFragment);
        } else {
            transaction.add(R.id.frame_main, targetFragment);
        }
        if (lastOneFragment != null) {
            transaction.hide(lastOneFragment);
        }
        lastOneFragment = targetFragment;
        transaction.commit();

    }

    private void initFragment() {
        mHomeFragment = new HomeFragment();
        mOnSellFragment = new OnSellFragment();
        mSearchFragment = new SearchFragment();
        mSelectFragment = new SelectFragment();
        mFragmentManager = getSupportFragmentManager();

    }

    @Override
    protected void initView() {
        bottomNavigationView = binding.mainNavigationBar;
    }

    @Override
    public void switch2Search() {
        //跳转到搜索界面
        bottomNavigationView.setSelectedItemId(R.id.search);
    }
}